package com.da.javatest.leetCode;

/**
 * @author chenlida
 * @date 2018/12/17 10:12
 * @description
 */
public class Code814 {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(0);
        root.left.left = new TreeNode(0);
        root.left.right = new TreeNode(0);
        root.right = new TreeNode(1);
        root.right.left = new TreeNode(0);
        root.right.right = new TreeNode(1);

        new Code814().pruneTree(root);
    }

    public TreeNode pruneTree(TreeNode root) {
        setNode(root);
        return root;
    }

    private TreeNode setNode(TreeNode root) {
        if (root == null) { return null; }
        if (root.left == null && root.right == null && root.val == 0) {
            return null;
        }
        root.left = setNode(root.left);
        root.right = setNode(root.right);
        return (root.left == null && root.right == null && root.val == 0) ? null : root;
    }
}
